home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / SUBARTIC / SUB_ARCT / CONSTRAI / LINEAR_E.JAV < prev    next >
Encoding:
Text File  |  1996-10-04  |  4.6 KB  |  139 lines

  1. package sub_arctic.constraints;
  2.  
  3. import sub_arctic.lib.manager;
  4. import sub_arctic.lib.sub_arctic_error;
  5.  
  6. /** 
  7.  * An external (heavyweight) constraint for computing a linear value
  8.  * (A*x + B for two fixed values A and B and one variable x).  This class 
  9.  * provides an example of how to do a stand-alone external constraint.
  10.  */
  11. public class linear_ext_constraint extends external_constraint { 
  12.  
  13.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  14.  
  15.   /** "A" coefficient in A*x+B equation. */
  16.   protected int _A;
  17.  
  18.   /** "A" coefficient in  A*x+B equation. */
  19.   public int A() {return _A;}
  20.  
  21.   /** Set the "A" coefficient in A*x+B equation. */
  22.   public void set_A(int v) {_A = v;}
  23.  
  24.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  25.  
  26.   /** "B" coefficient in A*x+B equation. */
  27.   protected int _B;
  28.  
  29.   /** "B" coefficient in A*x+B equation. */
  30.   public int B() {return _B;}
  31.  
  32.   /** Set the "B" coefficient in A*x+B equation. */
  33.   public void set_B(int v) {_B = v;}
  34.  
  35.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  36.  
  37.   /** Provider part that gives the value we compute from */
  38.   protected provider_part_ref _target;
  39.  
  40.   /** Provider part that gives the value we compute from */
  41.   public provider_part_ref target() {return _target;}
  42.  
  43.   /** Set the provider part that gives the value we compute from */
  44.   public void set_target(provider_part_ref targ) 
  45.     {
  46.       /* detach from any old target */
  47.       if (_target != null && _target.obj != null)
  48.     _target.obj.detach_dependent(_target.part_num, this,0); 
  49.   
  50.         /* establish new target and attach to that */
  51.       _target = targ;
  52.       if (_target != null && _target.obj != null)
  53.     _target.obj.attach_dependent(_target.part_num, this,0); 
  54.     }
  55.  
  56.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  57.   
  58.   /** 
  59.    * Constructor.
  60.    * @param int            a      the A value in A*x+B. 
  61.    * @param int            b      the B value in A*x+B. 
  62.    * @param value_provider v_obj  the object we obtain the x value from in 
  63.    *                  A*x+B.
  64.    * @param int            v_part the part number of the above object that we 
  65.    *                              obtain the x value from in A*x+B.
  66.    */
  67.   public linear_ext_constraint(int a, int b, value_provider v_obj, int v_part)
  68.     {
  69.       /* initialize */
  70.       _A = a;
  71.       _B = b;
  72.       _target = new provider_part_ref(v_obj, v_part);
  73.  
  74.       /* attach to our target */
  75.       if (_target != null && _target.obj != null)
  76.         _target.obj.attach_dependent(_target.part_num, this,0); 
  77.     }
  78.  
  79.    //had:
  80.    //*@exception bad_value if an invalid value_provider or part number is given.
  81.  
  82.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  83.  
  84.   /** Finalize by cutting off from any target */
  85.   public void finalize()
  86.     {
  87.       set_target(null);
  88.     }
  89.  
  90.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  91.  
  92.   /** 
  93.    * Compute our value as A*x+B where is x is an integer obtained from 
  94.    * our target provider part.  Here part_number is ignored since we only 
  95.    * provide one value. <p>
  96.    *
  97.    * @param int part_number the part number of the value being requested from
  98.    *                        us.  Since we have only one part we ignore this.
  99.    * @return Object an Integer value providing the result.
  100.    */
  101.    public Object get_value(int part_number) 
  102.      {
  103.        Object raw_v;
  104.  
  105.        /* if we have no target, we're in trouble */
  106.        if (target() == null || target().obj == null) 
  107.      throw new sub_arctic_error("Null target object in linear_ext_constraint");
  108.  
  109.        /* get the value and type check */
  110.        raw_v = target().obj.get_value(target().part_num);
  111.        if (!(raw_v instanceof Integer))
  112.      throw new sub_arctic_error("Value is not Integer in linear_ext_constraint");
  113.  
  114.        /* do the computation */
  115.        return new Integer(A() * (((Integer)raw_v).intValue()) + B());
  116.  
  117.      }
  118.  
  119.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  120.  
  121. }
  122.   
  123. /*=========================== COPYRIGHT NOTICE ===========================
  124.  
  125. This file is part of the subArctic user interface toolkit.
  126.  
  127. Copyright (c) 1996 Scott Hudson and Ian Smith
  128. All rights reserved.
  129.  
  130. The subArctic system is freely available for most uses under the terms
  131. and conditions described in 
  132.   http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html 
  133. and appearing in full in the lib/interactor.java source file.
  134.  
  135. The current release and additional information about this software can be 
  136. found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
  137.  
  138. ========================================================================*/
  139.